package com.cocos.game.zdkj.log;

import android.os.Bundle;
import android.util.Log;

import com.cocos.game.DeviceUtil;
import com.cocos.game.MAppCacheManager;
import com.cocos.game.MApplication;
import com.cocos.game.zdkj.net.NetHelper;
import com.cocos.game.ZDKJUtils;
import com.cocos.game.zdkj.model.DeviceLogRecord;
import com.google.firebase.analytics.FirebaseAnalytics;

import java.util.HashMap;
import java.util.Map;

public class LogReporter {

    /**
     * AB面获取结果上报
     * @param resultType AB结果
     * @param adjustNetwork 归因信息
     */
    public static void logAppType(ABResultType resultType, String adjustNetwork) {
        String eventName = "";
        if (resultType == ABResultType.SUCCESS) {
            eventName = "init_adjust_ok_" + adjustNetwork;
        } else if (resultType == ABResultType.FAIL) {
            eventName = "init_adjust_fail";
        } else if (resultType == ABResultType.DELAY) {
            eventName = "init_adjust_delay";
        }
        Map<String, String> params = getCommonParams(eventName);
        NetHelper.getInstance().logReport(eventName, params);
        FirebaseAnalytics.getInstance(MApplication.instance).logEvent(eventName, paramsToBundle(params));
    }


    public static void logReport(EventType eventType, Map<String, String> extraParams) {
        try {
            Map<String, String> allParams = getCommonParams(eventType.getName());
            if (extraParams != null){
                allParams.putAll(extraParams);
            }
            Log.d("LogReport"," event="+eventType.getName() + " params="+allParams);

            if (eventType.isSelfServer()){
                NetHelper.getInstance().logReport(eventType.getName(), allParams);
            }
            FirebaseAnalytics.getInstance(MApplication.instance).logEvent(eventType.getName(), paramsToBundle(allParams));
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    /**
     * 透传cocos埋点
     * @param eventName 事件名
     * @param params cocos透传参数 "userid@N:110;nickname@S:张三;age@N:17.65;level@N:30;freshman@B:false"
     */
    public static void logHelpCocosReport(String eventName,String params) {

        Map<String, String> allParams = getCommonParams(eventName);
        if (params != null && !params.isEmpty()){
            String[] cocosParams = params.split(";");
            Map<String,String> extraParams = new HashMap<>();
            for (String cocosParam:cocosParams) {
                try {
                    String[] temp = cocosParam.split("@");
                    if ("userid".equals(temp[0]) || "userId".equals(temp[0])){
                        MAppCacheManager.setUserId(temp[1].split(":")[1]);
                    }
                    if("gameInitStart".equals(eventName)){
                        DeviceLogRecord deviceLogRecord = MAppCacheManager.getDeviceLogRecord();
                        deviceLogRecord.setGameInitStart(true);
                        MAppCacheManager.setDeviceLogRecord(deviceLogRecord);
                    }
                    if("gameInitFinish".equals(eventName)){
                        DeviceLogRecord deviceLogRecord = MAppCacheManager.getDeviceLogRecord();
                        deviceLogRecord.setGameInitFinish(true);
                        MAppCacheManager.setDeviceLogRecord(deviceLogRecord);
                    }
                    if("gameStart".equals(eventName)){
                        DeviceLogRecord deviceLogRecord = MAppCacheManager.getDeviceLogRecord();
                        deviceLogRecord.setGameStart(true);
                        MAppCacheManager.setDeviceLogRecord(deviceLogRecord);
                    }
                    extraParams.put(temp[0], temp[1].split(":")[1]);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }

            allParams.putAll(extraParams);
        }

        NetHelper.getInstance().logReport(eventName, allParams);
        FirebaseAnalytics.getInstance(MApplication.instance).logEvent(eventName, paramsToBundle(allParams));
    }

    public enum ABResultType {
        SUCCESS,
        FAIL,
        DELAY
    }

    private static Map<String, String> getCommonParams(String eventName) {
        Map<String, String> params = new HashMap<>();
        params.put("time", ZDKJUtils.getCurrentTime());
        params.put("userId", MAppCacheManager.getUserId());
        params.put("country", ZDKJUtils.getLocale().getCountry());
        params.put("version", MApplication.VERSION_CODE() + "");
        params.put("product", MApplication.APPLICATION_ID());
        params.put("userInitDay", MAppCacheManager.getUserMaxInitTime());
        params.put("timeStamp", System.currentTimeMillis() + "");
        params.put("deviceId", DeviceUtil.getDeviceId());
        params.put("day", ZDKJUtils.getCurrentUTCDate());
        params.put("eventName", eventName);
        params.put("adid", MAppCacheManager.getADID());
        return params;
    }

    private static Bundle paramsToBundle(Map<String, String> params) {
        Bundle bundle = new Bundle();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            bundle.putString(entry.getKey(), entry.getValue());
        }
        return bundle;
    }
}
